package cn.silver.module.monitor.service.impl;

import cn.silver.framework.core.utils.MapstructUtils;
import cn.silver.framework.log.event.OperEvent;
import cn.silver.framework.mybatis.service.impl.BaseServiceImpl;
import cn.silver.module.monitor.domain.LogOper;
import cn.silver.module.monitor.mapper.LogOperMapper;
import cn.silver.module.monitor.bean.LogOperBean;
import cn.silver.module.monitor.param.OperParam;
import cn.silver.module.monitor.service.ILogOperService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 操作日志 服务层处理
 *
 * @author Silver Zhou
 */
@Service
@RequiredArgsConstructor
public class LogOperServiceImpl extends BaseServiceImpl<LogOperMapper, LogOper, OperParam, LogOperBean> implements ILogOperService {


    /**
     * 操作日志记录
     *
     * @param operEvent 操作日志事件
     */
    @Async
    @EventListener
    public void recordOper(OperEvent operEvent) {
        LogOperBean operLog = MapstructUtils.convert(operEvent, LogOperBean.class);
        // 远程查询操作地点
        this.insert(operLog);
    }

//	@Override
//	public ResultPage<LogOperModel> selectPage(PageModel page, LogOper operLog) {
//		Map<String, Object> params = operLog.getParams();
//		LambdaQueryWrapper<LogOper> lqw = new LambdaQueryWrapper<LogOper>()
//				.like(StringUtils.isNotBlank(operLog.get()), SysOperLog::getTitle, operLog.getTitle())
//				.eq(operLog.getBusinessType() != null && operLog.getBusinessType() > 0,
//						SysOperLog::getBusinessType, operLog.getBusinessType())
//				.func(f -> {
//					if (ArrayUtil.isNotEmpty(operLog.getBusinessTypes())) {
//						f.in(SysOperLog::getBusinessType, Arrays.asList(operLog.getBusinessTypes()));
//					}
//				})
//				.eq(operLog.getStatus() != null,
//						SysOperLog::getStatus, operLog.getStatus())
//				.like(StringUtils.isNotBlank(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
//				.between(params.get("beginTime") != null && params.get("endTime") != null,
//						SysOperLog::getOperTime, params.get("beginTime"), params.get("endTime"));
//		if (StringUtils.isBlank(pageQuery.getOrderByColumn())) {
//			pageQuery.setOrderByColumn("oper_id");
//			pageQuery.setIsAsc("desc");
//		}
//		Page<SysOperLogVo> page = baseMapper.selectVoPage(pageQuery.build(), lqw);
//		return ResultPage.build(page.getRecords(), page.getTotal());
//	}

    /**
     * 新增操作日志
     *
     * @param bo 操作日志对象
     */
//	@Override
//	public void insertOperlog(SysOperLogBo bo) {
//		SysOperLog operLog = MapstructUtils.convert(bo, SysOperLog.class);
//		operLog.setOperTime(new Date());
//		baseMapper.insert(operLog);
//	}

    /**
     * 查询系统操作日志集合
     *
     * @param operLog 操作日志对象
     * @return 操作日志集合
     */
//	@Override
//	public List<SysOperLogVo> selectOperLogList(SysOperLogBo operLog) {
//		Map<String, Object> params = operLog.getParams();
//		return baseMapper.selectVoList(new LambdaQueryWrapper<SysOperLog>()
//				.like(StringUtils.isNotBlank(operLog.getTitle()), SysOperLog::getTitle, operLog.getTitle())
//				.eq(operLog.getBusinessType() != null && operLog.getBusinessType() > 0,
//						SysOperLog::getBusinessType, operLog.getBusinessType())
//				.func(f -> {
//					if (ArrayUtil.isNotEmpty(operLog.getBusinessTypes())) {
//						f.in(SysOperLog::getBusinessType, Arrays.asList(operLog.getBusinessTypes()));
//					}
//				})
//				.eq(operLog.getStatus() != null && operLog.getStatus() > 0,
//						SysOperLog::getStatus, operLog.getStatus())
//				.like(StringUtils.isNotBlank(operLog.getOperName()), SysOperLog::getOperName, operLog.getOperName())
//				.between(params.get("beginTime") != null && params.get("endTime") != null,
//						SysOperLog::getOperTime, params.get("beginTime"), params.get("endTime"))
//				.orderByDesc(SysOperLog::getId));
//	}

    /**
     * 批量删除系统操作日志
     *
     * @param operIds 需要删除的操作日志ID
     * @return 结果
     */
//	@Override
//	public int deleteOperLogByIds(Long[] operIds) {
//		return baseMapper.deleteByIds(Arrays.asList(operIds));
//	}

    /**
     * 查询操作日志详细
     *
     * @param operId 操作ID
     * @return 操作日志对象
     */
//	@Override
//	public SysOperLogVo selectOperLogById(Long operId) {
//		return baseMapper.selectVoById(operId);
//	}

    /**
     * 清空操作日志
     */
    @Override
    public void cleanOperLog() {
        baseMapper.delete(new LambdaQueryWrapper<>());
    }
}
